<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}




$id=empty($_REQUEST['id'])?0:(int)$_REQUEST['id'];
if(empty($id)){

    error_html("没有找到加工单！","","");
}else{
    $workinfo=$ezhand->select("workman",'*','id="'.$id.'"');
    if(empty($workinfo["orderid"])){
        error_html("没有找到加工单！","","");
    }
    if($workinfo["zjstatus"]==2 or $workinfo["zjstatus"]==4){
        
        error_html("此加工单已经质检完成，不能重复提交！","","");
    }
    if($workinfo["status"]<>2){
        
        error_html("此加工单还没完成，不能提交质检！","","");
        
    }
    
 
            
    if($_REQUEST["sub"]=="save"){
        
        
        $codenumb=trim($_POST["codenumb"]);
        $thistoken=md5(EZKEY.$id.$codenumb.$_SESSION['EZ_Admin']['id']);
        if(empty($_POST["token"])){
           jumpajax('300','质检提交失败！',''); 
        }
        if($thistoken<>$_POST["token"]){
            jumpajax('300','质检提交失败！','');
            
        }
        
        $setstatus=intval($_POST["setstatus"]);
        if(empty($setstatus)){
            jumpajax('300','质检状态没有选择！',''); 
        }
        if($setstatus<>1 and $setstatus<>2){
            jumpajax('300','质检状态没有选择！','');
        }
        
        $goodsedlist=$_POST["goodsed"];
        if(!is_array($goodsedlist)){
            jumpajax('300','质检通过数量填写有误！',''); 
        }
        $goodsmsg=$_POST["goodsmsg"];
        $chkuser=$_POST["chkuser"];
        $goodsnumlist=$_POST["goods"];
            
        $chktemplist=$ezhand->select_all("workman_checklist","goodsid,msg,checkman,sum(checknum) as checknum,sum(goodsnum) as goodsnum","workmanid=".$id." group by goodsid");
        while(list($k,$v)=each($chktemplist)){
                $ctid=$v["goodsid"];   
                $chklist[$ctid]=$v;
        }
                    
        $goodslisttemp=$ezhand->select_all("shopping3_goods","*","1");
        while(list($k,$v)=each($goodslisttemp)){
            $gid=$v["id"];
            $goodslist[$gid]=$v;    
            
        }
            
        
        $where="o.id=og.orderid and o.id in (".$workinfo["orderid"].")  ";
        
        $ordergoods=$ezhand->select_all("shopping3_order o,shopping3_order_goods og",'og.goodsid,sum(og.total) as total',''.$where.'  group by og.goodsid ');
        
        $deallist=array();
        while(list($k,$v)=each($ordergoods)){
            $goodsid=$v["goodsid"];
            $newnum=intval($goodsedlist[$goodsid]);
            $goodsnum=intval($goodsnumlist[$goodsid]);
            
         
                        
            
            if($newnum<0){
                
                jumpajax('300',"“".$goodsinfo["title"]."” 质检数量不能小于0！",'');
            }
            $goodsinfo=$goodslist[$goodsid];

            if($chklist[$goodsid]["checknum"]>0){
                if($newnum==0){         //如果已经质检过一次，本次质检数为0，则不保存此记录
                    continue;
                }
                $newnum1=$newnum+$chklist[$goodsid]["checknum"];
            }else{
                $newnum1=$newnum;
            }
                        
            if($setstatus==2){
                
            if($newnum1<$v["total"]){
                
                jumpajax('300',"“".$goodsinfo["title"]."” 未达到订单量，不能提交全部质检通过，请选择部分质检通过！",''); 
            }
            }
            $unchecknnum=$goodsnum-$newnum;
            if($unchecknnum<0){
                
                 jumpajax('300',"“".$goodsinfo["title"]."” 质检数量不得大于加工完成数量！",'');
            }
            $rgoods=array(
            "goodsid"=>$goodsid,
            "goodssn"=>$goodsinfo["goodssn"],
            "title"=>$goodsinfo["title"],
            "ordernum"=>$v["total"],
            "goodsnum"=>$goodsnum,
            "workmanid"=>$id,
            "userid"=>$_SESSION['EZ_Admin']['id'],
            "username"=>$_SESSION['EZ_Admin']['name'],
            "ctime"=>time(),
            "checknum"=>$newnum,
            "unchecknum"=>$unchecknnum,
            "msg"=>$goodsmsg[$goodsid],
            "checkman"=>$chkuser[$goodsid]
            
            );
            
            $deallist[]=$rgoods;
        }       
        if(empty($deallist)){
            jumpajax('300','质检确认数据为空！',''); 
        }
        $i=0;
        while(list($k,$rd)=each($deallist)){
            
            $ezhand->insert("workman_checklist",$rd);
            
            //入库
            $sql="update ce_shopping3_goods  set amount=amount+".$rd["checknum"]." where id=".$rd["goodsid"];
            
            $ezhand->query($sql);
            
            $i++;
            
        }
        if($setstatus==1){
            $setstatus=3;
            $ezhand->workman_log($id,"113");
        }elseif($setstatus==2){
            $setstatus=2;
            $ezhand->workman_log($id,"112");
        }
        if($setstatus==3){
        $rdate=array("zjstatus"=>$setstatus,"status"=>"1","chk_username"=>$_SESSION['EZ_Admin']['name'],"chk_time"=>time());
            
        }else{
            
        $rdate=array("zjstatus"=>$setstatus,"chk_username"=>$_SESSION['EZ_Admin']['name'],"chk_time"=>time());
            
        }
        $ezhand->update("workman",$rdate,"id=".$id);
        if($workinfo["parentid"]>0){
            
            $ezhand->update("workman",$rdate,"parentid=".$id);
            
        }
         
        
    $od=array("shipstatus"=>5);
    $ezhand->update("shopping3_order",$od,"workid=".$id);
            
        
        jumpajax('200','质检确认提交成功！','index.php?mod=checkpart&act=list&zjstatus='.$setstatus,'forward'); 
    }
    
    
    $codenumb=$workinfo["code_numb"];
    $ordernum=$workinfo["ordernum"];
    $work_items=$ezhand->select_all("workman_items",'*','wid="'.$id.'"');
    
    $goodsnum=$workinfo["subitems"];
    $metnum=$workinfo["metnums"];
    
    $where="o.id=og.orderid and o.id in (".$workinfo["orderid"].") and o.status in (1,2)  ";
    
    $ordergoods=$ezhand->select_all("shopping3_order o,shopping3_order_goods og",'og.goodsid,sum(og.total) as total',''.$where.'  group by og.goodsid ');

    $ordergoodstemp=$ordergoods;
    
    $goodslisttemp=$ezhand->select_all("shopping3_goods","*","1");
    while(list($k,$v)=each($goodslisttemp)){
        $gid=$v["id"];
        $goodslist[$gid]=$v;    
        
    }
    /*
    
            //取得食材明细
        $materiallisttemp=$ezhand->select_all("material","*","1");
        while(list($k,$v)=each($materiallisttemp)){
            $pid1=$v["id"];
            $materiallist[$pid1]=$v;    
            
        }
        
        //取得菜品对应食材的明细
        $goodsmetlist=array();
        
        $metnamelist=array();
        $metlist=$ezhand->select_all("goods_items","*","1 group by tid,mid");
        while(list($k,$v)=each($metlist)){
         $tid=$v["tid"];
        
         $mid=$v["mid"];
        
         $goodsmetlist[$tid][]=$v;
         $metnamelist[$mid]=$v["mname"]; 
        }
         
*/
        $donenumlist=array();
        $donelist=array();
        $donelisttemp=$ezhand->select_all("workman_done","*","workmanid=".$id." order by goodsid asc,id asc");
        $chklist=array();
        while(list($k,$v)=each($donelisttemp)){
            $gid=$v["goodsid"];
            $donelist[$gid]=$v;
            if(!isset($donenumlist[$gid])){
                $donenumlist[$gid]=0;
            }
            $donenumlist[$gid]=$donenumlist[$gid]+$v["goodsnum"];
            $chklist[$gid]["checknum"]=0;
            $chklist[$gid]["goodsnum"]=0;
        }        
        $blnum=$ezhand->count("workman","parentid=".$id);  
        $bllistrow= $ezhand->select_all("workman","*","parentid=".$id);
        
        //$checklist=$ezhand->select_all("workman_checklist","*","workmanid=".$id." order by goodsid asc,id asc");
        
        
        $chktemplist=$ezhand->select_all("workman_checklist","goodsid,msg,checkman,sum(checknum) as checknum,sum(goodsnum) as goodsnum","workmanid=".$id." group by goodsid");
        while(list($k,$v)=each($chktemplist)){
                $ctid=$v["goodsid"];   
                $chklist[$ctid]=$v;
        }
                    

}

$token=md5(EZKEY.$id.$codenumb.$_SESSION['EZ_Admin']['id']);
?>